<template>
  <div
    ref="childWinRef"
    class="child-win"
    :style="{
      color: themeStyle[theme].textColor1,
      border: `1px solid ${themeStyle[theme].borderColor1}`,
      backgroundColor: themeStyle[theme].backgroundColor1
    }"
  >
    <div
      class="title"
      :style="{
        backgroundColor: themeStyle[theme].backgroundColor2
      }"
    >
      <span class="title-text">{{ title }}</span>
      <div class="electron-title-icon">
        <div class="icon-item" @click="handle('hide')">
          <svg-icon name="funHide" size="0.8em" :color="themeStyle[theme].textColor1"></svg-icon>
        </div>
        <div class="icon-close" @click="handle('close')">
          <svg-icon
            name="funClose"
            size="0.8em"
            :color="themeStyle[theme].textColor1"
            hover-color="#fff"
          ></svg-icon>
        </div>
      </div>
    </div>

    <template v-if="key === 'svgSetting'">
      <svg-setting></svg-setting>
    </template>
    <template v-if="key === 'jsonData'">
      <json-data :data="data"></json-data>
    </template>
    <template v-if="key === 'randomFunction'">
      <random-fun></random-fun>
    </template>
    <template v-if="key === 'refinement'">
      <machine-refinement></machine-refinement>
    </template>
    <template v-if="key === 'lineIndicators'">
      <line-indicators></line-indicators>
    </template>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useStyleStore } from '@renderer/store/globalStyle/style'
import SvgSetting from './components/svgSetting/SvgSetting.vue'
import JsonData from './components/jsonData/JsonData.vue'
import RandomFun from './components/randomFun/RandomFun.vue'
import MachineRefinement from './components/machineRefinement/MachineRefinement.vue'
import LineIndicators from './components/lineIndicators/LineIndicators.vue'

const winId = ref<number | null>(null)
const key = ref<string>('')
const title = ref<string>('子窗口标题')
const data = ref<any>(null)

onMounted(() => {
  //监听主进程传过来的初始化数据
  window.electron.ipcRenderer.on('initData', (event, params) => {
    winId.value = params.id
    title.value = params.title
    data.value = params?.data
    key.value = params.key
  })

  changeTheme()
})

const handle = (params: string): void => {
  const data = {
    id: winId.value,
    key: key.value,
    type: params
  }
  window.electron.ipcRenderer.send('childWinSize', data)
}

const globalStyleStore = useStyleStore()
const { theme, themeStyle } = storeToRefs(globalStyleStore)
const childWinRef = ref<HTMLDivElement | null>(null)
watch(
  () => theme.value,
  () => {
    changeTheme()
  }
)
const changeTheme = (): void => {
  const hoverColor1 = themeStyle.value[theme.value].btnHoverColor3
  //设置--btnHoverColor
  childWinRef.value?.style.setProperty('--btnHoverColor', hoverColor1)
}
</script>

<style scoped lang="scss">
.child-win {
  width: calc(100% - 2px);
  height: calc(100vh - 2px);
  position: relative;
  font-size: $font-size-medium;
  --btnHoverColor: #eee;

  .title {
    width: 100%;
    height: 30px;
    background-color: #eee;
    -webkit-app-region: drag; /* 允许拖拽 */
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title-text {
      margin-left: 10px;
    }
    .electron-title-icon {
      display: flex;
      align-items: center;
      gap: 5px;
      -webkit-app-region: no-drag; /* 禁止拖拽 */
      .icon-item {
        cursor: pointer;
        width: 40px;
        height: 30px;
        cursor: pointer;
        &:hover {
          background-color: var(--btnHoverColor);
        }
      }
      .icon-close {
        cursor: pointer;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 30px;
        &:hover {
          background-color: $close-button-hover-background-color;
        }
      }
    }
  }
}
</style>
